﻿using Framework;

namespace TemplateEngine.TranslateCommand
{
    internal class CallTranslate : ITranslate
    {
        public string Translate(TemplateNode node, System.Xml.XmlNode xmlNode)
        {
            string name = node.Attributes["name"];
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new TemplateEngineException("模板定义错误，call截点必须定义name属性！" + node.startNode);
            }

            name = name.Trim();
            var body = node.Document.RootLst.Find(delegate(TemplateNode root)
            {
                string bodyName = root.Attributes["name"];
                return name == bodyName;
            });

            if (body == null)
            {
                throw new TemplateEngineException("没有找到名字是" + name + "的body节点");
            }

            return body.TranslateNode(xmlNode);
        }
    }
}
